<?php


namespace app\api\model;
use think\Model;

class Message extends Model
{
    public static function getMessageList($userid){
        $Message = new Message();
        $info = $Message->where("address_id",$userid)->alias('a')
            ->join('stu_user uf','a.user_id_sender = uf.user_id','left')//发件人
            ->join('stu_user us','a.address_id = us.user_id','left')//收件人
            ->field('a.*,uf.username as sendname,us.username as putname')
            ->select();
        return $info;
    }

    public static function getMessageById($id){
        $Message = new Message();
        $info = $Message->where("id",$id)->alias('a')
            ->join('stu_user uf','a.user_id_sender = uf.user_id','left')//发件人
            ->join('stu_user us','a.address_id = us.user_id','left')//收件人
            ->field('a.*,uf.username as sendname,us.username as putname')
            ->find();
        return $info;
    }

    public static function sendMessage($userid,$type,$classid=0,$messageTitle,$messageContent){
        $User = new User();
        switch ($type) {
            //1所有学生   2所有教师和学生   3单个班级的所有学生
            case 1:
                $Student = $User->where(['status'=>1,'group_id'=>10001])->column('user_id');
                break;
            case 2:
                $Student = $User->where(['status'=>1,'group_id'=>10001])->whereOr(['status'=>1,'group_id'=>10002])->column('user_id');
                break;
//            case 3:
//                $Student = $User->where(['status'=>1,'group_id'=>10001])->select();
//                break;

        }
        $data['user_id_sender'] =  $userid;
        $data['message_title'] =  $messageTitle;
        $data['message_content'] =  $messageContent;
        $data['type'] =  1;
        $data['status'] =  1;
        foreach ($Student as $value) {
            $data['address_id'] =  $value;
            self::create($data);
        }
        return true;

    }
}